var app = getApp();
var ajax = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
var wxUtil = require('../../utils/wxUtil.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');

Page(common.extendPage(MPage, {
    data: {
        dataLoaded: false,
        myHidden: true,
        modalHidden: true,
        addrList: [],
        addressId: '',
        name: '',
        tel: '',
        addr: '',
        province: '',
        city: '',
        county: '',
        provinceId: '',
        cityId: '',
        countyId: '',
    },
    onLoad: function(options) {
        // this._orderOptions = options.orderOptions;
    },
    onShow: function() {
        this._initAddr();
    },
    /**
     * 初始化地址列表
     */
    _initAddr: function() {
        var _self = this;
        ajax.P('/openapi/CAddress/getAll', {}, function(data) {
            var d = data.result;
            if (d.data) {
                _self.setData({
                    addrList: d.data
                })
            } else {
                _self.setData({
                    addrList: d
                })
            }
            var addrList = _self.data.addrList;
            addrList.some(function(item) {
                if (item.isDefault == 1) {
                    item.selected = true;
                }
            })
            _self.setData({
                addrList,
                dataLoaded: true
            })
        })
    },
    /**
     * 选择地址
     */
    _select: function(e) {
        var _self = this;
        var index = parseInt(e.currentTarget.dataset.index);
        var addrList = this.data.addrList;
        addrList.forEach(function(item, index) {
            if (addrList[index].selected) {
                addrList[index].selected = false;
                _self.setData({
                    addrList,
                })
            }
        })
        addrList[index].selected = true;

        /*
        ** 这个是第一版的旧数据，优化后，数据不需要，留在这里是为了溯源 
        ***
         _self.setData({
             addressId: addrList[index].id,
             name: addrList[index].reName,
             tel: addrList[index].rePhone,
             addr: addrList[index].detailAddr,
             province: addrList[index].provinceName,
             city: addrList[index].cityName,
             county: addrList[index].districtName,
             addrList,
         })
        **/

        _self.setData({
            addressId: addrList[index].id,
            addrList,
        })
        var formId = e.detail.formId;

        common.submitForm(formId);

        /*
        ** 这个是第一版的旧数据，优化后，数据不需要，留在这里是为了溯源 
        ***

        // var orderOptions = JSON.parse(this._orderOptions);
        // var url = '/pages/confirmOrder/confirmOrder?addressId=' + _self.data.addressId;

        // Object.keys(orderOptions).forEach(function(key) {
        //     url += '&' + key + '=' + orderOptions[key];
        // });
        // wx.redirectTo({
        //     url
        // })

        **/
        _self._goPrevPage();
    },
    /**
     * 页面跳转
     */
    _jump: function(evt) {
        common.jump(evt);
    },
    /**
     * 设置上一个页面的数据,并跳转到上个页面
     */
    _goPrevPage: function() {
        var _self = this;
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
            optionAddrId: _self.data.addressId
        })
        wx.navigateBack({
            delta: 1
        });
    }
}))